package com.yangrong.newsservice.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.yangrong.newsservice.entity.TbArticlesEntity;
import com.yangrong.newsservice.service.TbArticlesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yangrong.common.utils.PageUtils;
import com.yangrong.common.utils.R;


/**
 * 文章表
 *
 * @author yangrong
 * @email 455833224@qq.com
 * @date 2020-09-04 11:09:13
 */
@RestController
@RequestMapping("article/tbarticles")
public class TbArticlesController {
    @Autowired
    private TbArticlesService tbArticlesService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = tbArticlesService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 根据文章类别、标签获取文章列表（不传params默认查询全部类别）
     *
     * @param params
     * @return
     */
    @RequestMapping("/getArticleList")
    public R getArticleList(@RequestParam Map<String, Object> params) {
        List<TbArticlesEntity> list = tbArticlesService.getArticleList(params);

        return R.ok().put("articleList", list);
    }

    /**
     * 根据文章类别、标签获取更多文章列表（不传params默认查询全部类别）
     *
     * @param params
     * @return
     */
    @RequestMapping("/getMoreArticleList")
    public R getMoreArticleList(@RequestParam Map<String, Object> params) {
        List<TbArticlesEntity> list = tbArticlesService.getMoreArticleList(params);

        return R.ok().put("moreArticleList", list);
    }

    /**
     * 根据文章类别、标签获取符合条件文章总数
     *
     * @param params
     * @return
     */
    @RequestMapping("/getArticleListSize")
    public R getArticleListSize(@RequestParam Map<String, Object> params) {
        Integer size = tbArticlesService.getArticleListSize(params);

        return R.ok().put("articleListSize", size);
    }

    /**
     * 获取文章内容
     *
     * @param params
     * @return
     */
    @RequestMapping("/getArticleContent")
    public R getArticleContent(@RequestParam Map<String, Object> params) {
        String content = tbArticlesService.getArticleContent(params);

        return R.ok().put("content", content);
    }

    @RequestMapping("/getArticleCarousel")
    public R getArticleCarousel(@RequestParam Map<String, Object> params) {
        List<TbArticlesEntity> list = tbArticlesService.getArticleCarousel(params);

        return R.ok().put("carouselList", list);
    }

    @RequestMapping("/getFlashArticle")
    public R getFlashArticle(@RequestParam Map<String, Object> params) {
        List<TbArticlesEntity> list = tbArticlesService.getFlashArticle(params);

        return R.ok().put("flashList", list);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        TbArticlesEntity tbArticles = tbArticlesService.getById(id);

        return R.ok().put("tbArticles", tbArticles);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody TbArticlesEntity tbArticles) {
        tbArticlesService.saveArticle(tbArticles);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody TbArticlesEntity tbArticles) {
        tbArticlesService.updateById(tbArticles);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        tbArticlesService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
